/**
 *  *2010-5-8 下午08:59:09  add 
 */
package com.base.myproject.client.busi.print;

import java.util.ArrayList;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.tools.fileupload.IUploader;
import com.base.myproject.client.ui.BaseFormPanel;
import com.base.myproject.client.ui.BaseGridPanel;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.base.myproject.client.ui.SingleFileUpload;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * @author Administrator 2010-5-8 javazgw@gmail.com
 */
public class BusiPrinter extends BaseJDBCPanel {

	public BusiPrinter() {
		super();
		init();
	}

	public void init() {

		initWidget();
		initbutton();

		this.registerMainStore(store);
		this.registerStore(store);
	}

	private void initbutton() {
		// TODO Auto-generated method stub

	}

	private void initWidget() {

		// this.setSize(400, 800);

//		 ContentPanel cp = new ContentPanel();
//		 cp.setLayout(new RowLayout(Orientation.VERTICAL));
//		 cp.add(createGrid(),new RowData(1,0.5));
//		 cp.add(createForm(),new RowData(1,-1));
//		 //cp.setHeight(300);

		// cp.setHeaderVisible(false);
		VerticalPanel v = new VerticalPanel();
		v.add(createGrid());
		v.add(createForm());

		// v.setWidth(1500);
		// v.setAutoWidth(true);
		// RootPanel.get("zgwtest").add(createGrid());

		this.add(v);
		// this.setAutoHeight(true);

	}

	BaseStore<BusiObject> store = null;

	BaseStore<BusiObject> getStore() {
		if (store == null) {
			createStore();
		}
		return store;
	}

	BaseStore<BusiObject> createStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("printicode");
		type.getField("printicode").setType(String.class);
		type.addField("printname");
		type.getField("printname").setType(String.class);
		type.addField("jspath");
		type.getField("jspath").setType(String.class);
		type.addField("busicode");
		type.getField("busicode").setType(String.class);
		type.addField("printjsppath");
		type.getField("printjsppath").setType(String.class);
		StoreInfo info = new StoreInfo();
		info.setTablename("printer");
		info.setKeyname(new String[] { "icode" });
		info
				.setSql("select icode,printicode,printname,jspath,busicode,printjsppath from printer");
		// info.setParent(mainstore);// 关联主表
		// info.setLinkparentcolumns(new String[] { "icode" }); // 关联主表字段
		// info.setLinkparent_chile_columns(new String[] { "upicode" });//
		// 关联主表字段
		store = new BaseStore<BusiObject>(type, info);
		return store;
	}

	BaseGridPanel gridpanel = null;

	BaseGridPanel createGrid() {
		CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());

//		ColumnConfig C1 = new ColumnConfig("icode", "icode", 80);
//		columns.add(C1);
		ColumnConfig C2 = new ColumnConfig("printicode", "打印编码", 80);
		columns.add(C2);
		ColumnConfig C3 = new ColumnConfig("printname", "打印格式名称", 80);
		columns.add(C3);
		ColumnConfig C4 = new ColumnConfig("jspath", "js路径", 80);
		columns.add(C4);
		ColumnConfig C5 = new ColumnConfig("busicode", "业务编码", 80);
		columns.add(C5);
		ColumnConfig C6 = new ColumnConfig("printjsppath", "打印jsp文件", 80);
		columns.add(C6);

		ColumnModel cm = new ColumnModel(columns);
		gridpanel = new BaseGridPanel(getStore(), cm,true,true);
		cbs.setSelectionMode(SelectionMode.SINGLE);
		gridpanel.getBg().setSelectionModel(cbs);
		gridpanel.setHeight(300);
		gridpanel.getBg().getView().setForceFit(true);
		return gridpanel;
	}

	BaseFormPanel form = null;

	BaseFormPanel createForm() {
		BaseFormPanel form = new BaseFormPanel(getStore());
		TableLayout tl = new TableLayout(4);

		TableData td = new TableData();
		td.setRowspan(1);
		td.setHorizontalAlign(HorizontalAlignment.LEFT);
		td.setVerticalAlign(VerticalAlignment.TOP);
		td.setPadding(2);
		TableData td2 = new TableData();
		td2.setRowspan(1);
		td2.setHorizontalAlign(HorizontalAlignment.LEFT);
		td2.setVerticalAlign(VerticalAlignment.TOP);
		td2.setPadding(2);
		td2.setColspan(3);

		
		
		form.setLayout(tl);
		form.setHeaderVisible(false);

//		form.add(new Label("icode:"), td);
//		NumberField C_num1 = new NumberField();
//		C_num1.setPropertyEditorType(Long.class);
//		C_num1.setName("icode");
//		form.add(C_num1, td);
		form.add(new Label("打印编码:"), td);
		TextField<String> C_num2 = new TextField<String>();
		C_num2.setName("printicode");
		form.add(C_num2, td);
		form.add(new Label("打印格式名称:"), td);
		TextField<String> C_num3 = new TextField<String>();
		C_num3.setName("printname");
		form.add(C_num3, td);
		form.add(new Label("js路径:"), td);
		TextField<String> C_num4 = new TextField<String>();
		C_num4.setName("jspath");
		form.add(C_num4, td);
		form.add(new Label("业务编码:"), td);
		TextField<String> C_num5 = new TextField<String>();
		C_num5.setName("busicode");
		form.add(C_num5, td);
		form.add(new Label("打印jsp文件:"), td);
		TextField<String> C_num6 = new TextField<String>();
		C_num6.setName("printjsppath");
		form.add(C_num6, td);
		
		form.add(new Label(""), td);
		form.add(new Label(""), td);
		
		final SingleFileUpload su = new SingleFileUpload();
		su.setValidExtensions(new String[] { "jsp" });
		su.setPrgBarInterval(100000);
		
		su.setOnStartHandler(new ValueChangeHandler<IUploader>() {

			@Override
			public void onValueChange(ValueChangeEvent<IUploader> event) {

				System.out.println("start=" + su.servletPath);
				su
						.setServletPath("gupld?filestorepath=printpath&uploadcommand=updateprint&icode="
								+ store.getValue("icode")
								+ "&storesql=update printer set printjsppath={filepath} where icode="
								+ store.getValue("icode"));
				System.out.println("start=" + su.servletPath);

				int curindex = store.getCurcowindex();
				store.reload();
				// store.gotorow(curindex);
				// gridpanel.getBg().

			}

		});

		su.setOnFinishHandler(new ValueChangeHandler<IUploader>() {

			@Override
			public void onValueChange(ValueChangeEvent<IUploader> event) {

				IUploader uploader = event.getValue();
				System.out.println("uploader=" + uploader);

				uploader = null;

			}

		});
		
		form.add(new Label("上传jsp文件:"), td);
		form.add(su, td2);
		
		form.inited();
		return form;
	};

	int sync = 0;
	Listener<BaseEvent> defaultlistener;

	Listener<BaseEvent> getListener() {
		if (defaultlistener != null) {
			return defaultlistener;
		}

		defaultlistener = new Listener<BaseEvent>() {
			public void handleEvent(BaseEvent be) {
				sync++;
				try {
					if (sync == 1) {
						if (be.getType() == Events.Select) {
							if (be.getSource().equals("按钮变量")) {

							}
						} else if (be.getType() == Events.Change) {
							if (be.getSource().equals("输入型变量")) {

							}
						} else if (be.getType() == Events.KeyUp) {
							if (be instanceof ComponentEvent) {
								if (((ComponentEvent) be).getKeyCode() == 13) // 回车
								{
								}
							}
						}
					}
				} finally {
					sync--;
				}
			}

		};
		return defaultlistener;
	}
}
